package com.xjzd.portrait.websocket;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

@Slf4j
public class LoggerQueue {

    public static final int QUEUE_SIZE = 10000;

    private static volatile LoggerQueue instance;

    private final BlockingQueue<String> queue = new LinkedBlockingQueue<>(QUEUE_SIZE);

    private LoggerQueue() {

    }

    public static LoggerQueue getInstance() {
        if (instance == null) {
            synchronized (LoggerQueue.class) {
                if (instance == null) {
                    instance = new LoggerQueue();
                }
            }
        }
        return instance;
    }

    public boolean push(String msg) {
        return queue.add(msg);
    }

    public String poll() {
        String result = null;
        try {
            result = queue.take();
        } catch (InterruptedException e) {
            log.warn(e.getMessage());
        }
        return result;
    }
}
